package github.hellocsl.gallerylayoutmanager;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

public class MainFraction extends Fraction {

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component mComponent = scatter.parse(ResourceTable.Layout_fraction_main, container, false);
        Image imageTitleBar = (Image) mComponent.findComponentById(ResourceTable.Id_image_titleBar_right);
        imageTitleBar.setVisibility(Component.VISIBLE);
        Button btnPageDemo = (Button) mComponent.findComponentById(ResourceTable.Id_btn_page_demo);
        Button btnGalleryDemo = (Button) mComponent.findComponentById(ResourceTable.Id_btn_gallery_demo);

        imageTitleBar.setClickedListener(component -> {
            Intent mIntents = new Intent();
            StringBuffer buffer=new StringBuffer();
            buffer.append("https:");
            buffer.append("//");
            buffer.append("github.com/BCsl");
            Operation operation = new Intent.OperationBuilder()
                    .withUri(Uri.parse(buffer.toString()))
                    .withAction(IntentConstants.ACTION_SEARCH)
                    .build();
            mIntents.setOperation(operation);
            getFractionAbility().startAbility(mIntents);
        });
        btnPageDemo.setClickedListener(component -> {
            ((MainAbility) getFractionAbility()).gotoFraction(MainAbility.SHOW_PAGER);
        });
        btnGalleryDemo.setClickedListener(component -> {
            ((MainAbility) getFractionAbility()).gotoFraction(MainAbility.SHOW_GALLERY);
        });
        return mComponent;
    }
}
